import { loginAPI, getUserInfoAPI, getUserImg } from '@/api'
import { getToken, setToken, removeToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    // 设置token
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    // 删除token
    delToken (state) {
      state.token = ''
      removeToken()
    },

    // 设置用户信息
    setUserInfo (state, data) {
      state.userInfo = data
    },
    delUserInfo (state) {
      state.userInfo = {}
    }
  },
  actions: {
    async loginActions ({ commit }, form) {
      const token = await loginAPI(form)
      console.log(token)
      commit('setToken', token)
      console.log(token)
      return token
    },

    async getUserInfoActions ({ commit }) {
      const data = await getUserInfoAPI()
      const userimg = await getUserImg(data.userId)
      console.log(data, userimg)
      commit('setUserInfo', { ...data, ...userimg })
    },

    loginOutActions ({ commit }) {
      commit('delToken')
      commit('delUserInfo')
    }
  }
}

// const getDefaultState = () => {
//   return {
//   }
// }
// const state = getDefaultState()

// const mutations = {
//   RESET_STATE: (state) => {
//     Object.assign(state, getDefaultState())
//   }
// }

// const actions = {
// }

// export default {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// }

